<?php

heartbeat_include('HeartbeatAccess');

/**
 * Class OgHeartbeat
 * Concrete class to prepare messages for the current user
 * and all of his/her relations.
 *
 */
class OgHeartbeat extends HeartbeatAccess {

  protected $_gid = 0;

  protected function hasAccess() {
    global $user;
    return og_is_group_member($this->_gid);
  }

  public function dressUpMessages(HeartbeatParser $heartbeat) {

    // Where the user id is a member of the group
    // and where the nid target is the group nid
    $this->_gid = arg(1);

    $sql = " AND ua.nid <> 0 AND ua.nid_target = %d ";

    $heartbeat->raw_messages = $this->resultSql($sql, array($this->_gid));

    return $heartbeat;
  }

  /**
   * Function to add a part of a sql to a query built by views UI
   *
   * @param object $view The view handler object by reference to add our part to the query
   */
  public function addViewQuery(&$view) {

    // Make the sql limited to the access
    $field = "$view->table_alias.$view->real_field";

    $view->query->set_where_group('AND','extra');

    $sql = "$view->table_alias.nid <> 0 AND $view->table_alias.nid_target %d";
    $view->query->add_where('extra', $sql, $this->_gid);
  }
}